﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DCS.ComLib.Config;
using VietThuongLMS.Services;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;
using System.Web.Security;
using VietThuongLMS.Areas.Admin.Models;

namespace VietThuongLMS.Areas.Admin.Controllers
{

    public class ThoiKhoaBieuController : Controller
    {
        //
        // GET: /Admin/ThoiKhoaBieu/
        //[Authorize(Roles = "Admin")]
        //[HttpGet, ValidateInput(false)]
        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Index()
        {
            //LMSDataEntities context = new LMSDataEntities();
            //var pcmr = new List<PhanCong>();
           
            //pcmr = context.PhanCongs.ToList();
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }
            PhanCongViewModel PhanCongList = PhanCongService.GetPhanCongList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page, -1);

            //int page = 1;
            //if (Request["p"] != null)
            //{
            //    page = int.Parse(Request["p"].ToString());
            //}

            //ThoiKhoaBieuViewModel ThoiKhoaBieuList = ThoiKhoaBieuService.GetThoiKhoaBieuList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page, maLopHoc);

            ////phân trang
            //Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "ThoiKhoaBieu", "Index", "?maLopHoc=" + maLopHoc + "&p=", ThoiKhoaBieuList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            //string pageLinks = pagination.GetPageLinks();
            //ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(PhanCongList);
        }

        //
        // GET: /Admin/ThoiKhoaBieu/Details/5
        //[Authorize(Roles = "Admin")]
        //public ActionResult Detail(int id, int maLopHoc)
        //{
        //    LMSDataEntities context = new LMSDataEntities();
        //    ThoiKhoaBieuMoRongModel viewModel = new ThoiKhoaBieuMoRongModel();

        //    viewModel.tkb = context.ThoiKhoaBieus.Join(context.LopHocs,
        //        tkb => tkb.MaLopHoc,
        //        lh => maLopHoc,
        //        (tkb, lh) => tkb).Where(tkb => tkb.ID == id).FirstOrDefault();


        //        //context.GiaoViens,
        //        //tkb => tkb.MaGiaoVien,
        //        //gv => gv.ID,
        //        //(tkb, gv) => tkb).Join(context.CaHocs,
        //        //tkb => tkb.MaCaHoc,
        //        //ch => ch.ID,
        //        //(tkb, ch) => tkb).Join(context.LopHocs,
        //        //tkb => tkb.MaLopHoc,
        //        //l => maLopHoc,
        //        //(tkb, l) => l).Join(context.MonHocs,
        //        //l => l.MaMonHoc,
        //        //m => m.ID,
        //        //(l, m) => l).Join(context.LoaiLopHocs,
        //        //l => l.MaLoaiLopHoc,
        //        //llh => llh.ID,
        //        //(l, llh) => l).Where(l => l.ID == id).FirstOrDefault();

        //    ////viewModel.ThoiKhoaBieuViewModel = ThoiKhoaBieuService.GetThoiKhoaBieuById(id);
        //    //viewModel.ThoiKhoaBieuViewModel = context.ThoiKhoaBieus.Join(context.ThoiKhoaBieus,
        //    //    tkb => tkb.MaThoiKhoaBieu,
        //    //    l => l.ID,
        //    //    (tkb, l) => tkb).Join(context.GiaoViens,
        //    //    tkb => tkb.MaGiaoVien,
        //    //    gv => gv.ID,
        //    //    (tkb, gv) => tkb).Join(context.CaHocs,
        //    //    tkb => tkb.MaCaHoc,
        //    //    ch => ch.ID,
        //    //    (tkb, ch) => tkb).FirstOrDefault();

        //    return View(viewModel);
        //}



        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Search()
        {
            LMSDataEntities context = new LMSDataEntities();
            IEnumerable<SelectListItem> lophoc = new SelectList(context.LopHocs.ToList(), "ID", "TenLopHoc");
            ViewData["lophoc"] = lophoc;

            return View();
        }

        //[HttpPost]
        //public ActionResult SearchResult()
        //{
        //    //string resultString = "";
        //    LMSDataEntities context = new LMSDataEntities();
        //    var pcmr = new List<PhanCong>();
        //    string ngay = "";
        //    string lophoc = "";
        //    DateTime ngayDB = new DateTime();

        //    if (!string.IsNullOrEmpty(Request.Form["ngay"]))
        //    {
        //        var con = new Utils.Converter();
        //        ngay = Request.Form["ngay"];
        //        ngayDB = con.StringToDate(ngay);
        //    }

        //    if (!string.IsNullOrEmpty(Request.Form["lophoc"]))
        //    {
        //        lophoc = Request.Form["lophoc"];
        //    }


        //    if (ngay == "" && lophoc == "")
        //    {
        //        pcmr = context.PhanCongs.ToList();
        //    }
        //    else if (ngay != "" && lophoc != "")
        //    {
        //        pcmr = context.PhanCongs.Where(a => a.NgayDay.Value.CompareTo(ngayDB) == 1 & (a.MaLopHoc == int.Parse(lophoc))).ToList();
        //    }
        //    else if (ngay != "" && lophoc == "")
        //    {
        //        pcmr = context.PhanCongs.Where(a => a.NgayDay.Value.CompareTo(ngayDB) == 1).ToList();
        //    }
        //    else if (ngay == "" && lophoc != "")
        //    {
        //        pcmr = context.PhanCongs.Where(a => a.MaLopHoc == int.Parse(lophoc)).ToList();
        //    }
            



        //    //var con = new Utils.Converter();
        //    //var ngay = con.StringToDate(s[0]);

        //    //var lh = context.PhanCongs.Where(a => a.NgayDay.Value.CompareTo(ngay)).ToList();

        //    //return PartialView(pcmr);
        //    //int page = 1;
        //    //if (Request["p"] != null)
        //    //{
        //    //    page = int.Parse(Request["p"].ToString());
        //    //}

        //    //var acvm = AccountService.GetTaiKhoanByOptionWithPagination(kw, op, 10, page);
        //    //phân trang
        //    //List<int> args = new List<int>();
        //    //args.Add(op);
        //    //ViewData["pageLinks"] = Utils.GeneralHTMLString.HTMLForJavaScriptPagination(pcmr.Count(), 10, page, "SearchResult", args);
        //    //return PartialView("pvAccountSearchingResult", acvm);

        //    //if (pcmr.Count > 0)
        //    //{

        //    //}

        //    return Json(new { count = pcmr.Count(), phancong = pcmr }, JsonRequestBehavior.AllowGet);
        //    //var json = Json(pcmr);
        //    //json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        //    //return json;
        //    //return  Json("dsadsadas", JsonRequestBehavior.AllowGet);
        //    //return PartialView(pcmr);
        //}

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult SearchResult(string s, string type)
        {
            LMSDataEntities context = new LMSDataEntities();
            var pc = new List<PhanCong>();
            Converter cv = new Converter();
            
            if (s != "")
            {
                switch (type)
                {
                    case "ngay":
                        DateTime ngay = cv.StringToDate(s);
                        pc = context.PhanCongs.Where(a => a.NgayDay == ngay).ToList();
                        break;
                    case "lophoc":
                        int maLop = int.Parse(s);
                        pc = context.PhanCongs.Where(a => a.MaLopHoc == maLop).ToList();
                        break;
                    default:
                        pc = context.PhanCongs.ToList();
                        break;
                }
            }
            else
            {
                pc = context.PhanCongs.ToList();
            }

            PhanCongViewModel pcm = new PhanCongViewModel(pc, pc.Count());
            return PartialView(pcm);

        }

        #region NHDuc
        /// <summary>
        /// Tra ve json cac lop hoc
        /// | Author: NHDuc
        /// | Created Date: 18/12/2013
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Admin,NhanVien")]
        public JsonResult GetTKB()
        {
            return Json(PhanCongService.GetPhanCongTKB());
        }
        #endregion

    }
}
